package com.yeung.exam;

import java.util.Scanner;

/*
15
0
1
2
3
#
#
4
#
5
#
#
#
#
6
#

 */
public class 深信服_创建二叉树 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = Integer.parseInt(sc.nextLine());
        int[] data = new int[count];
        for (int i = 0; i < count; i++) {
            String tm = sc.nextLine();
            if (tm.equals("#")) data[i] = -1;
            else data[i] = Integer.parseInt(tm);
        }
        //
        TreeNode root = createTree(new TreeNode(data[0]), data, 0, data.length );
        preOrder(root);
    }

    static TreeNode createTree(TreeNode node, int data[], int i, int len) {
        if (2 * i + 1 < len) {
            TreeNode lc, rc;
            if ((2 * i + 1) < len) {
                //左孩子
                lc = new TreeNode(data[2 * i + 1]);
                node.lChild = lc;
                node.lChild = createTree(node.lChild, data, 2 * i + 1, len);
            }
            if ((2 * i + 2) < len) {
                //有孩子chuang
                rc = new TreeNode(data[2 * i + 2]);
                node.rChild = rc;
                node.rChild = createTree(node.rChild, data, 2 * i + 2, len);
            }
        } else {
            node.lChild = node.rChild = null;
        }
        return node;
    }

    static void preOrder(TreeNode node) {
        if (node != null) {
            if (node.val == -1) System.out.println("#");
            else
                System.out.println(node.val);
            preOrder(node.lChild);
            preOrder(node.rChild);
        }
    }
}


